package domain;

import java.io.File;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import persistence.ImportConfig;
 /**
 * This class handles everything related to start, stopping and restarting the HTTP/HTTPS deamons.
 * @author HTTPDragon Team
 */
public class DomainController extends Observable implements Observer{
    private char[] sslPass;
    private DragonEngine deamon;
    private DragonEngineS deamonS;
    private ServerConfig config;
    private File configFile = new File("config.xml");
    private ExecutorService executor;
    private static final Logger logger = Logger.getLogger("HTTPDragon Log");
    private DomainController dc;
    /**
     * This method determines whether or not a a domaincontroller already exists.
     * Create a new DomainController if needed.
     * @return DomainController
     */
    public DomainController getInstance(){ 
        if(dc == null){ 
            dc = new DomainController();
            logger.addHandler(ErrorAndLogHandler.getInstance());
        } 
        return dc; 
    }
     /**
     * This method gives the current ServerConfig
     * @return ServerConfig The current configuration instance
     */
    public File getConfigFile() {
        return configFile;
    }
    
    private DragonEngine getDeamon() {
        return deamon;
    }
    private DragonEngineS getDeamonS() {
        return deamonS;
    }
    
    private void setDeamons(DragonEngine deamon,DragonEngineS deamonS) {
        this.deamon = deamon;
        this.deamonS = deamonS;
    } 
     /**
     * This method starts the HTTP deamons
     */
    public void serverStart(){         
       executor  = Executors.newCachedThreadPool();
       config = new ImportConfig(configFile).getConfig();
       setDeamons(new DragonEngine(config), new DragonEngineS(config,sslPass));
       getDeamon().addObserver(this);
       getDeamonS().addObserver(this);
       executor.execute(deamon);  
       executor.execute(deamonS);
    }
    
     /**
     * This method restarts the HTTP deamons
     */
    public void serverRestart(){
        serverStop();
        serverStart();
    }
    
     /**
     * This method stops the HTTP deamons
     */
    public void serverStop(){
        if(deamon.isRunning())
            deamon.shutdown();
        if(deamonS.isRunning())
            deamonS.shutdown();
        executor.shutdown();
    }
    
    // Server status as String
    
     /**
     * This method returns the current HTTP and HTTPS server status 
     * @return String the current HTTP and HTTPS server status
     */
    public String serverStatus(){
       String httpStatus = deamon.isRunning() ? "Alive":"Stopped";
       String httpsStatus = deamonS.isRunning() ? "Alive":"Stopped";
       return "[HTTP:" + httpStatus + "] [HTTPS:" + httpsStatus +"]";
    }
    // Server status as boolean
     /**
     * This method returns the OR of the HTTP and HTTPS server status 
     * @return boolean the OR of the HTTP and HTTPS server status
     */
    public boolean serverStatusBoolean(){
       if(deamon.isRunning()|deamonS.isRunning()){
        return true;
       }
        return false;
    }

     /**
     * This method sets the current SSL keyfile password 
     * @param pass The SSL keyfile password
     */
    public void setSSLPassPhrase(String pass) {
        sslPass = pass.toCharArray();
    }

     /**
     * This method passes on that the server(s) have changed state 
     * @param o Observable deamon
     * @param arg Arguments to pass
     */
    @Override
    public void update(Observable o, Object arg) {        
            this.setChanged();
            this.notifyObservers(); 
    }
}
